#include "DistanceMeasure.h"

#if SAT_H0610X
_m30x_type m30x_Family[Mx30_ParameterNum];
_m30x_Parameter m30x_Parameter;
_SlaveAskCode SlaveAskCode;
_MasterCmd MasterCmd;

//unsigned char Cmd_ReadDistance[] = {0x10,0x81,0x91};
unsigned char Cmd_CheckSelf[] = {0xEB,0X90,0X02,0X03,0X01,0x81,'\r'};//自检
unsigned char Cmd_ReadOneDistance[] = {0xEB,0X90,0X02,0X03,0X02,0x82,'\r'};//测量一次


void Init_Mx30Parameter(_m30x_type *m30x_Family)
{
	m30x_Family[_m30x_StartTest].TX_FrameByteNum = 4;
	m30x_Family[_m30x_StopTest].TX_FrameByteNum =3;
	m30x_Family[_m30x_SlaveState].TX_FrameByteNum = 3;//?????;
	m30x_Family[_m30x_DistanceRet].TX_FrameByteNum =3;
	m30x_Family[_m30x_Bound].TX_FrameByteNum = 7;
	m30x_Family[_m30x_Addr].TX_FrameByteNum= 7;
	m30x_Family[_m30x_OutputMode].TX_FrameByteNum = 7;
	m30x_Family[_m30x_IOVoltage].TX_FrameByteNum = 7;
	m30x_Family[_m30x_SlaveParam].TX_FrameByteNum = 2;
	m30x_Family[_m30x_ErrorCode].TX_FrameByteNum = 0;
	m30x_Family[_m30x_Control].TX_FrameByteNum = 3;
	
	m30x_Family[_m30x_StartTest].RX_FrameByteNum = 3;
	m30x_Family[_m30x_StopTest].RX_FrameByteNum =0;
	m30x_Family[_m30x_SlaveState].RX_FrameByteNum = 5;
	m30x_Family[_m30x_DistanceRet].RX_FrameByteNum = 7;
	m30x_Family[_m30x_Bound].RX_FrameByteNum = 0;
	m30x_Family[_m30x_Addr].RX_FrameByteNum = 0;
	m30x_Family[_m30x_OutputMode].RX_FrameByteNum = 0;
	m30x_Family[_m30x_IOVoltage].RX_FrameByteNum = 0;
	m30x_Family[_m30x_SlaveParam].RX_FrameByteNum = 6;
	m30x_Family[_m30x_ErrorCode].RX_FrameByteNum = 3;
	
	m30x_Family[_m30x_Control].RX_FrameByteNum = 0;

}


void Handle_Usart2Date(void)
{
	uint8_t SlaveAskCode = SlaveAskCode;
	static uint8_t rxbuf[Usart2_RXBuffSize] = {0};
	uint8_t temp,date0,date1,date2,date3;
	uint16_t ret;

		if(xQueueReceive(Distance_QueueHandle,(void*)rxbuf,10) == pdTRUE)
		{
			for(uint8_t num=0; num<Usart2_RXBuffSize; num++)
				Usart2_RXBuff[num] = 0;
			
			SlaveAskCode = rxbuf[4];
			switch(SlaveAskCode)
			{
				case _Cmd_SelfCheck :
					MyArrayCopy(rxbuf, m30x_Family[_m30x_DistanceRet].RX_BUFF);
					if((m30x_Family[_m30x_DistanceRet].RX_BUFF[5]&0xff) == 0)//设备正常
					{
						;
					}
					else
					{
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;
					}
					break;
				
				case _Cmd_ContinueMeasure :

					break;
				case _Cmd_OneMeasure :
					m30x_Family[_m30x_DistanceRet].Flag_WaitUse = true;
					MyArrayCopy(rxbuf, m30x_Family[_m30x_DistanceRet].RX_BUFF);
					if((m30x_Family[_m30x_DistanceRet].RX_BUFF[5]&0xff) == 0x76)//距离数据有效
					{
						ret = (uint16_t)m30x_Family[_m30x_DistanceRet].RX_BUFF[6]<<8 | (uint16_t)m30x_Family[_m30x_DistanceRet].RX_BUFF[7];
						//ret/=10;
						if(ret>=9999) ret=0;
						
						DecToHexToAscii(ret,0,OsdRefresh_Family[_Distance].Module_addr,1);
						AutoCompletion(OsdRefresh_Family[_Distance].Module_addr, 4, 0x30);//高位不足 补0
						SmallToBig(OsdRefresh_Family[_Distance].Module_addr);
						
						OsdRefresh_Family[_Distance].Module_addr[4] = osd_M;//单位
						OsdRefresh_Family[_Distance].Module_addr[5] = '\r';
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;
					}
					
					else if(m30x_Family[_m30x_DistanceRet].RX_BUFF[5]&0x80)//系统故障
					{
						//ret = 0;
						OsdRefresh_Family[_Distance].Module_addr[0] = osd_E;
						OsdRefresh_Family[_Distance].Module_addr[1] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[2] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[3] = osd_O;
						OsdRefresh_Family[_Distance].Module_addr[4] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[5] = '\r';
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;
					}
					
					else if(m30x_Family[_m30x_DistanceRet].RX_BUFF[5]&0x01)//报警
					{
						//ret = 0;
						OsdRefresh_Family[_Distance].Module_addr[0] = osd_A;
						OsdRefresh_Family[_Distance].Module_addr[1] = osd_L;
						OsdRefresh_Family[_Distance].Module_addr[2] = osd_A;
						OsdRefresh_Family[_Distance].Module_addr[3] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[4] = osd_M;
						OsdRefresh_Family[_Distance].Module_addr[5] = '\r';
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;
					}
					else
					{
						OsdRefresh_Family[_Distance].Module_addr[0] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[1] = osd_E;
						OsdRefresh_Family[_Distance].Module_addr[2] = osd_T;
						OsdRefresh_Family[_Distance].Module_addr[3] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[4] = osd_Y;
						OsdRefresh_Family[_Distance].Module_addr[5] = '\r';
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;
					}
					
					break;
					
				case _Cmd_StopMeasure :
					
					break;
				
				case _Cmd_SetFrequence :

					break;

				case _Cmd_QueryPara :

					break;

				case _Cmd_QueryLaserNum :

					break;
				
				default :
					break;
			}
		}
}


#elif LR6000

_m30x_type m30x_Family[Mx30_ParameterNum];
_m30x_Parameter m30x_Parameter;
_SlaveAskCode SlaveAskCode;
_MasterCmd MasterCmd;
//unsigned char Cmd_ReadDistance[] = {0x10,0x81,0x91};

unsigned char Cmd_CheckSelf[] = {0xEE,0X16,0X02,0X03,0X01,0x04,'\r'};//自检

unsigned char Cmd_ReadOneDistance[] = {0xEE,0X16,0X02,0X03,0X02,0x05,'\r'};//测量一次


void Handle_Usart2Date(void)
{
	uint8_t SlaveAskCode = SlaveAskCode;
	static uint8_t rxbuf[Usart2_RXBuffSize] = {0};
	uint8_t temp,date0,date1,date2,date3;
	uint16_t ret;

		if(xQueueReceive(Distance_QueueHandle,(void*)rxbuf,10) == pdTRUE)
		{
			for(uint8_t num=0; num<Usart2_RXBuffSize; num++)
				Usart2_RXBuff[num] = 0;
			
			SlaveAskCode = rxbuf[4];
			switch(SlaveAskCode)
			{
				case _Cmd_Equipment_self_test :
					MyArrayCopy(rxbuf, m30x_Family[_m30x_DistanceRet].RX_BUFF);
					if((m30x_Family[_m30x_DistanceRet].RX_BUFF[7] & 0xff) == 0)//设备正常
					{
						;
					}
					else
					{
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;
					}
					break;
				
				case _Cmd_Set_first_and_last_multiple_targets :

					break;
				
				case _Cmd_Single_measurement :
					
					m30x_Family[_m30x_DistanceRet].Flag_WaitUse = true;
					MyArrayCopy(rxbuf, m30x_Family[_m30x_DistanceRet].RX_BUFF);
				
					switch(m30x_Family[_m30x_DistanceRet].RX_BUFF[5] & 0xff)
					{
						case 0x00:
							
						ret = (uint16_t)m30x_Family[_m30x_DistanceRet].RX_BUFF[6]<<8 | (uint16_t)m30x_Family[_m30x_DistanceRet].RX_BUFF[7];
						//ret/=10;
						if(ret>=9999) ret=0;
						
						DecToHexToAscii(ret,0,OsdRefresh_Family[_Distance].Module_addr,1);
						AutoCompletion(OsdRefresh_Family[_Distance].Module_addr, 4, 0x30);//高位不足 补0
						SmallToBig(OsdRefresh_Family[_Distance].Module_addr);
						
						OsdRefresh_Family[_Distance].Module_addr[4] = osd_M;//单位
						OsdRefresh_Family[_Distance].Module_addr[5] = '\r';
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;
							
							break;
						case 0x01:
							
						ret = (uint16_t)m30x_Family[_m30x_DistanceRet].RX_BUFF[6]<<8 | (uint16_t)m30x_Family[_m30x_DistanceRet].RX_BUFF[7];
						//ret/=10;
						if(ret>=9999) ret=0;
						
						DecToHexToAscii(ret,0,OsdRefresh_Family[_Distance].Module_addr,1);
						AutoCompletion(OsdRefresh_Family[_Distance].Module_addr, 4, 0x30);//高位不足 补0
						SmallToBig(OsdRefresh_Family[_Distance].Module_addr);
						
						OsdRefresh_Family[_Distance].Module_addr[4] = osd_M;//单位
						OsdRefresh_Family[_Distance].Module_addr[5] = '\r';
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;
							break;
						case 0x02:
							
						ret = (uint16_t)m30x_Family[_m30x_DistanceRet].RX_BUFF[6]<<8 | (uint16_t)m30x_Family[_m30x_DistanceRet].RX_BUFF[7];
						//ret/=10;
						if(ret>=9999) ret=0;
						
						DecToHexToAscii(ret,0,OsdRefresh_Family[_Distance].Module_addr,1);
						AutoCompletion(OsdRefresh_Family[_Distance].Module_addr, 4, 0x30);//高位不足 补0
						SmallToBig(OsdRefresh_Family[_Distance].Module_addr);
						
						OsdRefresh_Family[_Distance].Module_addr[4] = osd_M;//单位
						OsdRefresh_Family[_Distance].Module_addr[5] = '\r';
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;
							break;
						case 0x03:
							
						ret = (uint16_t)m30x_Family[_m30x_DistanceRet].RX_BUFF[6]<<8 | (uint16_t)m30x_Family[_m30x_DistanceRet].RX_BUFF[7];
						//ret/=10;
						if(ret>=9999) ret=0;
						
						DecToHexToAscii(ret,0,OsdRefresh_Family[_Distance].Module_addr,1);
						AutoCompletion(OsdRefresh_Family[_Distance].Module_addr, 4, 0x30);//高位不足 补0
						SmallToBig(OsdRefresh_Family[_Distance].Module_addr);
						
						OsdRefresh_Family[_Distance].Module_addr[4] = osd_M;//单位
						OsdRefresh_Family[_Distance].Module_addr[5] = '\r';
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;
							break;
						case 0x04:
						OsdRefresh_Family[_Distance].Module_addr[0] = osd_E;
						OsdRefresh_Family[_Distance].Module_addr[1] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[2] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[3] = osd_O;
						OsdRefresh_Family[_Distance].Module_addr[4] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[5] = '\r';
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;
						
							break;
						case 0x05:
							
						ret = (uint16_t)m30x_Family[_m30x_DistanceRet].RX_BUFF[6]<<8 | (uint16_t)m30x_Family[_m30x_DistanceRet].RX_BUFF[7];
						//ret/=10;
						if(ret>=9999) ret=0;
						
						DecToHexToAscii(ret,0,OsdRefresh_Family[_Distance].Module_addr,1);
						AutoCompletion(OsdRefresh_Family[_Distance].Module_addr, 4, 0x30);//高位不足 补0
						SmallToBig(OsdRefresh_Family[_Distance].Module_addr);
						
						OsdRefresh_Family[_Distance].Module_addr[4] = osd_M;//单位
						OsdRefresh_Family[_Distance].Module_addr[5] = '\r';
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;
							break;
						default:
                            break;							
					
					}
						
					
					
					break;
					
				case _Cmd_Continuous_ranging :
					
					break;
				
				case _Cmd_Stop_ranging :

					break;

				case Cmd_Distance_measurement_anomaly :
						OsdRefresh_Family[_Distance].Module_addr[0] = osd_E;
						OsdRefresh_Family[_Distance].Module_addr[1] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[2] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[3] = osd_O;
						OsdRefresh_Family[_Distance].Module_addr[4] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[5] = '\r';
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;

					break;

				
				
				default :
					break;
			}
		}
}


#elif STA_H0306X

_m30x_type m30x_Family[Mx30_ParameterNum];
_m30x_Parameter m30x_Parameter;
_SlaveAskCode SlaveAskCode;
_MasterCmd MasterCmd;


unsigned char Cmd_CheckSelf[] = {0xEE,0X07,0X01,0X00,0X00,0x00,0X00,0X00,0X01, '\r'};//自检

unsigned char Cmd_ReadOneDistance[] = {0xEE,0X07,0X0B,0X00,0X00,0x00,0X00,0X00,0X0B, '\r'};//测量一次


void Handle_Usart2Date(void)
{
	uint8_t SlaveAskCode = SlaveAskCode;
	static uint8_t rxbuf[Usart2_RXBuffSize] = {0};
	uint8_t temp,date0,date1,date2,date3;
	uint16_t ret;

		if(xQueueReceive(Distance_QueueHandle,(void*)rxbuf,10) == pdTRUE)
		{
			for(uint8_t num=0; num<Usart2_RXBuffSize; num++)
				Usart2_RXBuff[num] = 0;
			
			SlaveAskCode = rxbuf[2];
			switch(SlaveAskCode)
			{
				case 0X00 :
					MyArrayCopy(rxbuf, m30x_Family[_m30x_DistanceRet].RX_BUFF);
					if((m30x_Family[_m30x_DistanceRet].RX_BUFF[4] & 0xff) == 0)//设备正常
					{
						;
					}
					else
					{
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;
					}
					break;
				
				case _Cmd_Set_first_and_last_multiple_targets :

					break;
				
				case 0X0B :
					
					m30x_Family[_m30x_DistanceRet].Flag_WaitUse = true;
					MyArrayCopy(rxbuf, m30x_Family[_m30x_DistanceRet].RX_BUFF);
				
					switch(m30x_Family[_m30x_DistanceRet].RX_BUFF[4] & 0xff)
					{
						case 0x00:
							
						ret = (uint16_t)m30x_Family[_m30x_DistanceRet].RX_BUFF[6]<<8 | (uint16_t)m30x_Family[_m30x_DistanceRet].RX_BUFF[5];
						//ret/=10;
						if(ret>=9999) ret=0;
						
						DecToHexToAscii(ret,0,OsdRefresh_Family[_Distance].Module_addr,1);
						AutoCompletion(OsdRefresh_Family[_Distance].Module_addr, 4, 0x30);//高位不足 补0
						SmallToBig(OsdRefresh_Family[_Distance].Module_addr);
						
						OsdRefresh_Family[_Distance].Module_addr[4] = osd_M;//单位
						OsdRefresh_Family[_Distance].Module_addr[5] = '\r';
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;
							
							break;
						case 0x08:
						OsdRefresh_Family[_Distance].Module_addr[0] = osd_E;
						OsdRefresh_Family[_Distance].Module_addr[1] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[2] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[3] = osd_O;
						OsdRefresh_Family[_Distance].Module_addr[4] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[5] = '\r';
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;							
						
							break;
						case 0x10:
						OsdRefresh_Family[_Distance].Module_addr[0] = osd_E;
						OsdRefresh_Family[_Distance].Module_addr[1] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[2] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[3] = osd_O;
						OsdRefresh_Family[_Distance].Module_addr[4] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[5] = '\r';
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;							
						
							break;
						case 0x20:
						OsdRefresh_Family[_Distance].Module_addr[0] = osd_E;
						OsdRefresh_Family[_Distance].Module_addr[1] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[2] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[3] = osd_O;
						OsdRefresh_Family[_Distance].Module_addr[4] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[5] = '\r';
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;
						
							break;
					
						default:
                            break;							
					
					}
						
					
					
					break;
					
				case _Cmd_Continuous_ranging :
					
					break;
				
				case _Cmd_Stop_ranging :

					break;

				case Cmd_Distance_measurement_anomaly :
						OsdRefresh_Family[_Distance].Module_addr[0] = osd_E;
						OsdRefresh_Family[_Distance].Module_addr[1] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[2] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[3] = osd_O;
						OsdRefresh_Family[_Distance].Module_addr[4] = osd_R;
						OsdRefresh_Family[_Distance].Module_addr[5] = '\r';
						OsdRefresh_Family[_Distance].Refresh_Flag = Refresh_yes;

					break;

				
				
				default :
					break;
			}
		}
}
#endif


void OsdShowDistance(void)
{
	Handle_Usart2Date();
}


void DistanceMesureOne(void)
{
	
	Uart2Send(Cmd_ReadOneDistance,MyStrlen(Cmd_ReadOneDistance));
}


void LaserPowerOFF(void)
{
	gpio_bits_write(GPIOB, GPIO_PINS_4, 0);//关闭激光电源
}


void LaserPowerON(void)
{
	gpio_bits_write(GPIOB, GPIO_PINS_4, 1);//打激光电源
}






